#include <kew/c-vm.h>
#include <kew/stdlib.h>
#include <glib.h>

 
static gpointer setup_top_continuation(KewVM *vm, KewDynamicEnvironment *env, KewThreadMainHook *hook, KewThreadMain main_function, gpointer main_function_data)
{
    gpointer result;
    
    // FIXME: set up continuation and add it as extension to env
    
    kew_env_add_extension(env, kew_top_continuation_key, top_continuation);
    
    result = KEW_THREAD_MAIN_CALL_HOOK(vm, env, hook->next, main_function, main_function_data);

    // FIXME: tear down continuation

    kew_env_remove_extension(env, kew_top_continuation_key);

    return result;
}

GHashTable *kew_boot_base_library(KewVM *vm)
{
    GHashTable *well_known_objects = g_hash_table_new(g_direct_hash, g_direct_equal);
    
    kew_top_continuation_key = (gpointer) "top continuation in KewDynamicEnvironment";
    
    /* FIXME: register selectors, types and return collection of well-known objects */
    
    kew_register_vm_entry_hook(vm, setup_top_continuation, NULL);

    return well_known_objects;
}

